$(document).ready(function(){
	//选择品牌
    $('.brand-list').on('click','p',function(){
        $(this).toggleClass('chose');
        var item=$('.brand-list p');
        var count=0;
        $(item).each(function(){
        	if($(this).hasClass('chose')){
        		count++;
        	}
        });
        if(count==item.length){
        	$('.brand-list span').addClass('chose');
        }
        if(count!=item.length){
        	$('.brand-list span').removeClass('chose');
        }
    });
    $('.brand-list').on('click','span',function(){
    	var item=$('.brand-list p');
    	$(this).toggleClass('chose');
    	if($(this).hasClass('chose')){
    		$(item).each(function(){
            	$(this).addClass('chose');
            });
    	}else{
    		$(item).each(function(){
            	$(this).removeClass('chose');
            });
    	}
    });
    $('.cate_list').on('mouseover','li',function(){
    	$(this).addClass('active').siblings('li').removeClass('active');
    	$('.type_block').css('display','block');
    	var id = $(this).attr('data-id');
    	$('.type_block').children("[data-id='"+id+"']").css('display','block').siblings('div').css('display','none'); 
    })
    $('.type_block').on('mouseover',function(){
    	$(this).css('display','block');
    	$('.type_block').on('mouseleave',function(){
        	$(this).css('display','none');
        	$('.cate_list').find('li').removeClass('active');
        })
    });
    $('.cate_block').on('mouseleave',function(){
    	$('.type_block').css('display','none');
    	$('.cate_list').find('li').removeClass('active');
    });
  //点击分类进入具体分类页面
    $('.cate_list').on('click','a',function(e){
    	e.preventDefault();
    	var id = $(this).parent('li').attr('data-id');
    	searchProduct(id);
    })
    $('.type_block').on('click','dd a',function(e){
    	e.preventDefault();
    	var id = $(this).attr('data-id');
    	searchProduct(id);
    }).on('click','dt a',function(e){
    	e.preventDefault();
    	var id = $(this).parent().attr('data-id');
    	searchProduct(id);
    });
    $('.search_product_list').on('click','li',function(){
    	var id = $(this).attr('data-id');
    	searchProduct(id);
    });
//    进入商品详情页面
    $('.product-list').on('click','.to_detail,.img-block,.product-txt>p',function(){
    	var id = $(this).parent().parent().find('button').attr('data-id');
    	window.location.href = path + "/product/product/goProductDetail?productId="+id;
    });
//    加载商品
    var id = GetQueryString("id");
    if(id){
    	$.ajax({
    		type:"post",
    		data:{categoryId:id},
    		url:path + "/product/product/productListByCategoryId",
    		success:function(data){
    			if(data.data){
	    			var list = data.data;
	    			var productList = "";
	    			for(var i = 0;i < list.length;i++){
						productList += "<div class='product-item special fl clearfix'>";
						productList += "<div>";
						productList += "<div class='img-block'>";
						productList += "<img src='/docs/"+list[i].productImg+"' alt=''/>";
						productList += "</div>";
						productList += "<div class='product-txt'>";
						productList += "<p>"+list[i].productName+"</p>";
						productList += "<p class='price'><b>￥"+list[i].minPrice+"-￥"+list[i].maxPrice+"</b></p>";
						productList += "</div>";
						productList += "<button class='to_detail'  data-id='"+list[i].productId+"'>查看详情</button>";
						productList += "</div>";
						productList += "</div>"
	    			};
	    			$('.product-list').html(productList);
    			}
    		},
    		error:function(){}
    	})
    }
//  加载分类
    $.ajax({
		type:"post",
		url:"/diconet/productCategory/findAllProdCategory",
		success:function(data){
			var typeList = data.data;
		    var cateList = "";
		    var cateBlock = "";
		    for(var i = 0;i<typeList.length;i++){
		    	if(typeList[i].parentId == ""){
		    		cateList += "<li data-id='"+typeList[i].id+"'><a href=''>"+typeList[i].categoryName+"</a></li>";
		    		cateBlock += "<div class='cate_item' data-id='"+typeList[i].id+"'></div>"
		    	}
		    	$('.cate_list').html(cateList);
		    	$('.type_block').html(cateBlock);
		    	
		    };
		    $('.cate_item').each(function(){
		    	var cateItem = "";
		    	var id = $(this).attr('data-id');
		    	for(var i = 0;i<typeList.length;i++){
		        	if(typeList[i].parentId == id){
		        		cateItem += "<dl class='type_item'>";
		        		cateItem += "<dt data-id='"+typeList[i].id+"'>";
		        		cateItem += "<a href=''>"+typeList[i].categoryName+"</a>";
		        		cateItem += "<i>></i>";
		        		cateItem += "</dt>";
		        		cateItem += "<dd class='type_detail'>";
		        		for(var j = 0;j<typeList.length;j++){
		        			if(typeList[j].parentId == typeList[i].id){
		        				cateItem += "<a href='' data-id='"+typeList[j].id+"'>"+typeList[j].categoryName+"</a>"
		        			}
		        		}
		        		cateItem += "</dd>";
		        		cateItem += "</dl>";
		        	}
		        };
		        $(this).html(cateItem);
		    })
		}
	})
});
//综合排序
$('.by-all').on('click',function(){
	var path = getRootPath();
	var productCategoryId = $('.product-item').children('li.chosed').attr('data-id');
	var distributorId = $('#search').attr('data-id');
	var productName = $('input[name="keywords"]').val()
	var conditionName ="sort_num"; 
	var conditionValue=1;
	window.location.href=path+"/product/getAllProductByCategoryId?productCategoryId="+productCategoryId+"&distributorId="+distributorId+"&productName="+productName+"&conditionName="+conditionName+"&conditionValue="+conditionValue; 
})
//价格
$(".by-price").on('click',function(){
	var path = getRootPath();
	var productCategoryId = $('.product-item').children('li.chosed').attr('data-id');
	var distributorId = $('#search').attr('data-id');
	var productName = $('input[name="keywords"]').val()
	if($(this).hasClass('high')){
		//由高到低
		var conditionName ="price"; 
		var conditionValue=0;
		window.location.href=path+"/product/getAllProductByCategoryId?productCategoryId="+productCategoryId+"&distributorId="+distributorId+"&productName="+productName+"&conditionName="+conditionName+"&conditionValue="+conditionValue; 
	}else if($(this).hasClass('low')){
		//由低到高
		var conditionName ="price"; 
		var conditionValue=1;
		window.location.href=path+"/product/getAllProductByCategoryId?productCategoryId="+productCategoryId+"&distributorId="+distributorId+"&productName="+productName+"&conditionName="+conditionName+"&conditionValue="+conditionValue; 
	}
	
});
//品牌搜索
$('.by-brand').on('click','button',function(){
	var path = getRootPath();
	var productCategoryId = $('.product-item').children('li.chosed').attr('data-id');
	var distributorId = $('#search').attr('data-id');
	var productName = $('input[name="keywords"]').val()
	var id=[];
	 $('.brand-list p').each(function(){
		 if($(this).hasClass('chose')){
			 id.push($(this).attr('data-id')); 
		 }
	 })
	var conditionValue=id.join(",");
	var conditionName ="brand"; 
	window.location.href=path+"/product/getAllProductByCategoryId?productCategoryId="+productCategoryId+"&distributorId="+distributorId+"&productName="+productName+"&conditionName="+conditionName+"&conditionValue="+conditionValue; 
})
//品牌选中
function f1(){
    var brand=$('.brand-list').attr('data-chose').split(',');
    for(var i=0;i<brand.length;i++){
    	$('.brand-list p').each(function(){
        	if($(this).attr('data-id')==brand[i]){
        		$(this).addClass('chose');
        	}
        });
    }
    
}
f1();
function searchProduct(id){
	window.location.href = path + "/product/product/goProductListPage?id="+id;
}
	
